package com.cloudkinto.controller.storage;


import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.service.storage.vo.FbaStorageAddReq;
import com.cloudkinto.service.storage.vo.FbaStorageEntity;
import com.cloudkinto.service.storage.vo.FbaStorageUpdateReq;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.cloudkinto.service.storage.FbaStorageService;

import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;

import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cloudkinto.base.BaseController;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author zhangyy
 * @since 2020-08-11
 */
@RestController
@Api(tags = "FAB库存模块")
@RequestMapping("/api/admin/fbaStorage")
public class FbaStorageController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(FbaStorageController.class);

    @Autowired
    private FbaStorageService service;

    @GetMapping("/addInit")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit() {
        return service.addInit();
    }

    @PostMapping("/add")
    @UserSessionRequired
    @ApiOperation(value = "新增Fba库存")
    public SingleResult addFbaStorage(@RequestBody @Validated FbaStorageAddReq req) {
        return SingleResult.success("新增成功",service.addFbaStorage(req));
    }

    @PutMapping("/update")
    @UserSessionRequired
    @ApiOperation(value = "修改Fba库存列表")
    public SingleResult addFbaStorage(@RequestBody @Validated FbaStorageUpdateReq req) {
        return SingleResult.success("修改成功",service.updateFbaStorage(req));
    }

    @PostMapping("")
    @UserSessionRequired
    @ApiOperation(value = "新增")
    public SingleResult add(@RequestBody @Validated List<FbaStorageEntity> req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("新增成功",service.add(req,userId));
    }

    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }

    @PutMapping("")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated Object req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功",service.update(req,userId));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除", notes = "根据id删除")
    public SingleResult delete(@PathVariable("id") Long id) {
        Long userId = getCurrentUserId();
        return SingleResult.success("删除成功",service.delete(id,userId));
    }

    @GetMapping("jumpToMap/{address}")
    @ApiOperation(value = "查看地图")
    public SingleResult jumpToMap(@PathVariable("address") String address) {
        return SingleResult.success("查询成功",service.jumpToMap(address));
    }

    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

    @GetMapping("/list")
    @ApiOperation(value = "查询", notes = "查询")
    @ApiImplicitParams(@ApiImplicitParam(name = "keyword", value = "关键字（模糊查询）"))
    public SingleResult list(HttpServletRequest request){
        Long companyId = getCompanyId();

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        map.put("keyword", keyword);
        return SingleResult.success("查询成功", service.getList(map));
    }

    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams({@ApiImplicitParam(name = "current", value = "当前页"),
            @ApiImplicitParam(name = "keyword", value = "地址或邮编模糊查询"),
            @ApiImplicitParam(name = "id", value = "仓库id"),
            @ApiImplicitParam(name = "areaId", value = "所属大区"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小")})
    public PageResult listPage(HttpServletRequest request){

        Map<String, String> map = new HashMap<>();
        String keyword = request.getParameter("keyword");
        String current = request.getParameter("current");
        String size = request.getParameter("pageSize");
        String id = request.getParameter("id");
        String areaId = request.getParameter("areaId");
        map.put("keyword", keyword);
        map.put("current", current);
        map.put("pageSize", size);
        map.put("id", id);
        map.put("areaId", areaId);
        return service.getListPage(map);
    }

}

